<template>
	<view class="container">
		<ad unit-id="adunit-f110a12a21e3ed3c" ad-type="video" ad-theme="white"></ad>
		<uni-forms err-show-type="undertext" :value="formData" ref="form" border="true" label-width="100" style="margin-top: 30rpx;">
			<uni-group :title="formData.gjInfo" v-if="formData.gjShow==1">
				<view style="padding: 20rpx;">
					<uni-tag :text="formData.gj" type="default"></uni-tag>
				</view>
			</uni-group>
			<uni-group :title="formData.kxjInfo" v-if="formData.kxjShow==1">
				<view style="padding: 20rpx;">
					<uni-tag :text="formData.kxj" type="default"></uni-tag>
				</view>
			</uni-group>
			<uni-group :title="formData.jzsInfo" v-if="formData.jzsShow==1">
				<view style="padding: 20rpx;">
					<uni-tag :text="formData.jzs" type="default"></uni-tag>
				</view>
			</uni-group>
			<view style="padding-top: 20rpx;" v-if="formData.titleNames.length>0"><button @click="refresh()">刷新</button></view>
			
			<uni-group title="头衔申请" v-if="formData.titleNames.length==0">
				<view v-if="formData.titleNames.length==0" style="padding: 20rpx;">
					头衔机器人已关闭，请咨询王国管理
				</view>
				</uni-group >
			<uni-group title="头衔申请" v-if="formData.titleNames.length>0">
				<uni-forms-item required label="头衔" name="title" v-if="formData.titleNames.length>0">
					<uni-data-checkbox v-model="formData.title" :localdata="formData.titleNames"></uni-data-checkbox>
				</uni-forms-item>
				<uni-forms-item required label="服务器编号" name="serverId"  v-if="formData.titleNames.length>0">
					<!-- <uni-easyinput type="number" trim="all" v-model="formData.serverId" :value="formData.serverId"
						placeholder="请输入服务器编号" /> -->
						<view style="padding-top: 20rpx;">{{formData.serverId}}</view>
				</uni-forms-item>
				<uni-forms-item label="失落之地编号" name="serverCode"  v-if="formData.titleNames.length>0">
					<uni-easyinput type="text" trim="all" v-model="formData.serverCode" placeholder="在KVK此项必填" />
				</uni-forms-item>
				<uni-forms-item required label="X坐标" name="coordinateX"  v-if="formData.titleNames.length>0">
					<uni-easyinput type="number" trim="all" v-model="formData.coordinateX" placeholder="请输入城堡X坐标"
						:value="formData.coordinateX" />
				</uni-forms-item>
				<uni-forms-item required label="Y坐标" name="coordinateY"  v-if="formData.titleNames.length>0">
					<uni-easyinput type="number" trim="all" v-model="formData.coordinateY" placeholder="请输入城堡Y坐标"
						:value="formData.coordinateY" />
				</uni-forms-item>
			</uni-group>
			<button type="primary" @click="submitForm('form')"  v-if="formData.titleNames.length>0">观看广告获取头衔</button>
		</uni-forms>
		<view>
			<!-- 提示信息弹窗 -->
			<uni-popup ref="message" type="message" :value="popupMessage">
				<uni-popup-message :type="popupMessage.msgType" :message="popupMessage.messageText" :duration="2000">
				</uni-popup-message>
			</uni-popup>
		</view>
	</view>

</template>

<script>
	let videoAd = null
	export default {
		data() {
			return {
				formData: {
					serverId: getApp().globalData.user.serverId,
					unionName: '',
					titleName: '',
					coordinateX: getApp().globalData.user.coordinateX,
					coordinateY: getApp().globalData.user.coordinateY,
					invitationCode: '',
					serverCode: getApp().globalData.user.serverCode,
					password: '',
					title: 0,
					gjShow: 1,
					kxjShow: 1,
					jzsShow: 1,
					gj: '',
					kxj: '',
					jzs: '',
					gjInfo: '公爵排队',
					kxjInfo: '科学家排队',
					jzsInfo: '建筑师排队',
					//titleNames: [{"value": 0,"text": "公爵"	},{"value": 1,"text": "科学家"},{"value": 2,"text": "建筑师"}]
					titleNames: []
				},
				popupMessage: {
					type: 'center',
					msgType: 'success',
					messageText: '这是一条成功提示',
					value: ''
				},
				rules: {
					coordinateX: {
						rules: [{
							required: true,
							errorMessage: '请输入联盟要塞X坐标',
						}, {
							format: 'int',
							errorMessage: '必须是数字'
						}]
					},
					coordinateY: {
						rules: [{
							required: true,
							errorMessage: '请输入联盟要塞Y坐标',
						}, {
							format: 'int',
							errorMessage: '必须是数字'
						}]
					}
				}
			}
		},
		onReady() {
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			submitForm: function(e) {
				let that = this
				// uni.showLoading()
if (videoAd) {
  videoAd.show().catch(() => {
    // 失败重试
    videoAd.load()
      .then(() => videoAd.show())
      .catch(err => {
        console.log('激励视频 广告显示失败')
      })
  })
}
videoAd.onClose(res => {
    // 用户点击了【关闭广告】按钮
    if (res && res.isEnded) {
      // 正常播放结束，可以下发游戏奖励
	  this.$refs.form.submit().then(addUser => {
	  	addUser.userId = getApp().globalData.user.id
	  	addUser.headImg = getApp().globalData.user.headImg
	  	addUser.nickName = getApp().globalData.user.nickName
	  	addUser.serverId = getApp().globalData.user.serverId
	  	addUser.password = getApp().globalData.user.password
	  	uni.request({
	  		url: getApp().globalData.url + '/api/title/applyTitle',
	  		method: "POST",
	  		header: {
	  			'content-type': 'application/json',
	  			'token': getApp().globalData.token
	  		},
	  		data: addUser,
	  		success: (res) => {
	  			if (res.data.code == "00000") {
	  				that.popupMessage.msgType = "success"
	  				that.popupMessage.messageText = "成功"
	  				that.$refs.message.open()
	  				getApp().globalData.user.coordinateX = addUser.coordinateX
	  				getApp().globalData.user.coordinateY = addUser.coordinateY
	  				getApp().globalData.user.serverCode = addUser.serverCode
	  				uni.requestSubscribeMessage({
	  					tmplIds: [
	  						'bzJWiieK2DR4_Cg5tRGdK3-ssw7PZxJGNiNuIUc04Lo'
	  					],
	  					success(res) {
	  						that.popupMessage.msgType = "success"
	  						that.popupMessage.messageText = "订阅成功"
	  						that.$refs.message.open()
	  					}
	  				})
	  
	  				uni.redirectTo({
	  					url: "/pages/applyTitle/applyTitle"
	  				})
	  
	  			} else {
	  				that.popupMessage.msgType = "error"
	  				that.popupMessage.messageText = res.data.msg
	  				that.$refs.message.open()
	  			}
	  		}
	  	});
	  	uni.hideLoading()
	  	console.log('表单数据信息：', addUser);
	  }).catch(err => {
	  	uni.hideLoading()
	  	console.log('表单错误信息：', err);
	  })
    } else {
      // 播放中途退出，不下发游戏奖励
    }
})


				
			},
			refresh: function(e) {
				uni.redirectTo({
					url: "/pages/applyTitle/applyTitle"
				})
			},
			formReset: function(e) {
				console.log('清空数据')
			},
			goHelp() {
				uni.navigateTo({
					url: "/pages/help/help"
				})
			},
			onLoad() {
				uni.request({
					url: getApp().globalData.url + '/api/title/queueInfo?serverId=' + getApp().globalData.user
						.serverId,
					header: {
						'content-type': 'application/json',
						'token': getApp().globalData.token
					},
					success: (res) => {
						if (res.data.code == "00000") {
							for (var i = 0; i < res.data.data.length; i++) {
								if (res.data.data[i].title == 0) {
									this.formData.gj = "排队人数:" + res.data.data[i].count
									if (res.data.data[i].index != null) {
										this.formData.gj += " 您排在第" + res.data.data[i].index + "位"
									}
								}
								if (res.data.data[i].title == 1) {
									this.formData.kxj = "排队人数:" + res.data.data[i].count
									if (res.data.data[i].index != null) {
										this.formData.kxj += " 您排在第" + res.data.data[i].index + "位"
									}
								}
								if (res.data.data[i].title == 2) {
									this.formData.jzs = "排队人数:" + res.data.data[i].count
									if (res.data.data[i].index != null) {
										this.formData.jzs += " 您排在第" + res.data.data[i].index + "位"
									}
								}

							}
						}
					}
				});
				uni.request({
					url: getApp().globalData.url + '/api/title/selectTitleByServerId?serverId=' + getApp().globalData.user
						.serverId,
					header: {
						'content-type': 'application/json',
						'token': getApp().globalData.token
					},
					success: (res) => {
						if (res.data.code == "00000") {
							this.formData.gjInfo += "(" + res.data.data.dukeKeepTime / 1000 + "秒/人)"
							this.formData.kxjInfo += "(" + res.data.data.scientistKeepTime / 1000 + "秒/人)"
							this.formData.jzsInfo += "(" + res.data.data.architectKeepTime / 1000 + "秒/人)"
							if (res.data.data.dukeSwitch == 1) {
								this.formData.titleNames.push({
									"value": 0,
									"text": "公爵"
								})
								this.formData.gjShow = 1
							} else {
								this.formData.gjShow = 0
							}
							if (res.data.data.scientistSwitch == 1) {
								this.formData.titleNames.push({
									"value": 1,
									"text": "科学家"
								})
								this.formData.kxjShow = 1
							} else {
								this.formData.kxjShow = 0
							}
							if (res.data.data.architectSwitch == 1) {
								this.formData.titleNames.push({
									"value": 2,
									"text": "建筑师"
								})
								this.formData.jzsShow = 1
							} else {
								this.formData.jzsShow = 0
							}
							
							if(this.formData.titleNames.length>0){
								this.formData.title=this.formData.titleNames[0].value
							}
						}
					}
				});
				if (wx.createRewardedVideoAd) {
				  videoAd = wx.createRewardedVideoAd({
					adUnitId: 'adunit-9c1f4d377b144e85'
				  })
				  videoAd.onLoad(() => {})
				  videoAd.onError((err) => {})
				  videoAd.onClose((res) => {})
				}

			}
		},
		onShareAppMessage: function(options) {
			var that = this;
			var shareObj = {
				path: '/pages/menu/menu'
			};
			return shareObj;
		}
	}
</script>

<style>
	.uni-form-item .title {
		padding: 20rpx 0;
	}

	.container {
		padding: 20rpx;
	}
</style>
